<?php 
class auto_message_adv_home 
{ 
	function auto_message_adv_main() 
	{ 
		global $mysql_conn,$v_file;
		$bot_id = $_GET['bot_id'];
		$list_page = $_GET['list_page'];
		$list_num = $_GET['list_num'];
		$sousuokey = $_GET['sousuokey'];
		if (empty($list_page)) $list_page = 1;
		if (empty($list_num)) $list_num = 10;
		if ($list_page < 5) {$list_page0 = 1;} else {$list_page0 = $list_page-4;}
		if ($list_page < 5) {$list_page1 = 1;} else {$list_page1 = $list_page-3;}
		if ($list_page < 5) {$list_page2 = 2;} else {$list_page2 = $list_page-2;}
		if ($list_page < 5) {$list_page3 = 3;} else {$list_page3 = $list_page-1;}
		if ($list_page < 5) {$list_page4 = 4;} else {$list_page4 = $list_page;}
		if ($list_page < 5) {$list_page5 = 5;} else {$list_page5 = $list_page+1;}
		if ($list_page < 5) {$list_page6 = 6;} else {$list_page6 = $list_page+2;}
		if ($list_page < 5) {$list_page7 = 7;} else {$list_page7 = $list_page+3;}
		if ($list_page < 5) {$list_page8 = 8;} else {$list_page8 = $list_page+4;}
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page0.")'><i class='fa fa-chevron-left'></i></button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page1."' onClick='list_page_go(".$list_page1.")'>".$list_page1."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page2."' onClick='list_page_go(".$list_page2.")'>".$list_page2."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page3."' onClick='list_page_go(".$list_page3.")'>".$list_page3."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page4."' onClick='list_page_go(".$list_page4.")'>".$list_page4."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page5."' onClick='list_page_go(".$list_page5.")'>".$list_page5."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page6."' onClick='list_page_go(".$list_page6.")'>".$list_page6."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page7."' onClick='list_page_go(".$list_page7.")'>".$list_page7."</button>";
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page8.")'><i class='fa fa-chevron-right'></i></button>";
		$list_limit = ($list_page-1)*$list_num.",".$list_num;
		if (empty($sousuokey)) {
			$sql = "SELECT * from `auto_message` WHERE `auto_message_bot` = '".$bot_id."' limit ".$list_limit;
		} else {
			$sql = "SELECT * from `auto_message` WHERE `auto_message_bot` = '".$bot_id."' and `auto_message_key` = '".$sousuokey."'";
		}
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$auto_message_id = $row['auto_message_id'];
			$auto_message_key = $row['auto_message_key'];
			$auto_message_body = $row['auto_message_body'];
			$automessagelist .= "<tr><td>";
			$automessagelist .= "    <input type='checkbox' class='i-checks' name='input[]'>";
			$automessagelist .= "</td>";
			$automessagelist .= "<td>".$auto_message_id."</td>";
			$automessagelist .= "<td>".$auto_message_key."</td>";
			$automessagelist .= "<td>".$auto_message_body."</td>";
			$automessagelist .= "<td>";
			$automessagelist .= "<a href='#' class='btn btn-white btn-xs' onClick='auto_message_edit(".$auto_message_id.");'><i class='fa fa-edit'></i> 编辑 </a>";
			$automessagelist .= "<a href='#' class='btn btn-white btn-xs' onClick='auto_message_del(".$auto_message_id.");'><i class='fa fa-trash-o'></i> 删除 </a>";
			$automessagelist .= "<a href='#' class='btn btn-white btn-xs' onClick='auto_message_use(".$auto_message_id.");'><i class='fa fa-trash-o'></i> 对象管理 </a>";
			$automessagelist .= "</td></tr>";
		}
		require($v_file);
	}
	function auto_message_add() 
	{ 
		global $mysql_conn,$v_file;
		$bot_id = $_GET['bot_id'];
		require($v_file);
	}
	function auto_message_add_do() 
	{ 
		global $mysql_conn;
		$bot_id = $_POST['bot_id'];
		$auto_message_key = $_POST['auto_message_key'];
		$auto_message_body = $_POST['auto_message_body'];
		$sql = "SELECT * from `auto_message` WHERE `auto_message_key` = '".$auto_message_key."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		if (empty($row)) {
			$sql="INSERT INTO auto_message (auto_message_id,auto_message_bot, auto_message_key, auto_message_body) VALUES (NULL,'$bot_id','$auto_message_key','$auto_message_body')";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			echo "2";//规则添加成功
		} else {
			echo  "1";//规则名已存在
		}
	}
	function auto_message_edit() 
	{ 
		global $mysql_conn,$v_file;
		$auto_message_id = $_GET['auto_message_id'];
		$bot_id = $_GET['bot_id'];
		$sql = "SELECT * from `auto_message` WHERE `auto_message_id` = '".$auto_message_id."' and `auto_message_bot` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$auto_message_key = $row['auto_message_key'];
		$auto_message_body = $row['auto_message_body'];
		require($v_file);
	}
	function auto_message_edit_do() 
	{ 
		global $mysql_conn;
		$auto_message_id = $_POST['auto_message_id'];
		$auto_message_bot = $_POST['auto_message_bot'];
		$auto_message_key = $_POST['auto_message_key'];
		$auto_message_body = $_POST['auto_message_body'];
		$sql = "SELECT * from `auto_message` WHERE `auto_message_key` = '".$auto_message_key."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		if (empty($row)) {
			$sql = "UPDATE `auto_message` SET `auto_message_key` = '".$auto_message_key."', `auto_message_body` = '".$auto_message_body."' WHERE `auto_message_id` ='".$auto_message_id."' and `auto_message_bot` ='".$auto_message_bot."';";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			echo "2";
		} else {
			echo  "1";//规则名已存在
		}
		
	}
	function auto_message_del_do() 
	{ 
		global $mysql_conn;
		$auto_message_id = $_POST['auto_message_id'];
		$sql = "DELETE from `auto_message` WHERE `auto_message_id` = '".$auto_message_id."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		echo "2";
	}
	function auto_message_use() 
	{ 
		global $mysql_conn,$v_file;
		$bot_id = $_GET['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		//获取现有的适用对象
		$auto_message_id = $_GET['auto_message_id'];
		$sql = "SELECT * from `auto_message` WHERE `auto_message_id` = '".$auto_message_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$auto_message_use = $row['auto_message_use'];
		$auto_message_use_arr = explode(',',$auto_message_use); 
		//获取好友数据
		$url = $server_url."openwx/get_friend_info?client=".$bot_id; 
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$friend_account = $value->{'account'};
			$friend_name = $value->{'name'};
			$friend_id = $value->{'id'};
			$friend_displayname = $value->{'displayname'};
			$friend_avatar_url = $server_url."openwx/get_avatar?id=".$friend_id."&client=".$bot_id; 
			$friend_category = $value->{'category'};
			if ($friend_category != "公众号") {
				$friend_list.= '<tr><td class="client-avatar"><img alt="image" src="'.$friend_avatar_url.'"> </td>';
				$friend_list.= '<td><a data-toggle="tab" href="weibot_friend.php?m=view?weibot_id='.$bot_id.'&friendid='.$friend_id.'" name="'.$friend_name.'" class="client-link">'.$friend_name.'</a></td>';
				$friend_list.= '<td> '.$friend_displayname.'</td>';
				$friend_list.= '<td class="contact-type"><i class="fa fa-comments"> </i></td>';
				$friend_list.= '<td> '.$friend_account.'</td>';
				if (in_array($friend_displayname, $auto_message_use_arr)) {
					$friend_list.= '<td class="client-status"><input type="checkbox" name="auto_message_usename" value="'.$friend_displayname.'" onClick="auto_message_uselist()" checked> </td></tr>';
				} else {
					$friend_list.= '<td class="client-status"><input type="checkbox" name="auto_message_usename" value="'.$friend_displayname.'" onClick="auto_message_uselist()" > </td></tr>';
				}
			}	
		}
		//获取群组数据
		$url = $server_url."openwx/get_group_info?client=".$bot_id;
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$group_displayname = $value->{'displayname'};
			$group_id = $value->{'id'};
			$group_member = $value->{'member'};
			$group_member_num = count($group_member);
			$group_avatar_url = $server_url."openwx/get_avatar?id=".$group_id."&client=".$bot_id; 			
			$group_list.= '<tr><td class="client-avatar"><img alt="image" src="'.$group_avatar_url.'"> </td>';
			$group_list.= '<td><a data-toggle="tab" href="weibot_friend.php?m=view$weibot_id='.$client.'&friendid='.$group_id.'" class="client-link">'.$group_displayname.'</a></td>';
			$group_list.= '<td></td><td class="contact-type"><i class="fa fa-comments"> </i></td>';
			$group_list.= '<td>成员：'.$group_member_num.' 人</td>';
			if (in_array($group_displayname, $auto_message_use_arr)) {
					$group_list.= '<td class="client-status"><input type="checkbox" name="auto_message_usename" value="'.$group_displayname.'"  onClick="auto_message_uselist()" checked> </td></tr>';		
				} else {
					$group_list.= '<td class="client-status"><input type="checkbox" name="auto_message_usename" value="'.$group_displayname.'"  onClick="auto_message_uselist()" > </td></tr>';		
				}
		}
		require($v_file);
	}
	function auto_message_use_do() 
	{
		global $mysql_conn,$v_file;
		$auto_message_id = $_POST['auto_message_id'];
		$auto_message_use = $_POST['auto_message_use'];
		$sql = "UPDATE `auto_message` SET `auto_message_use` = '".$auto_message_use."' WHERE `auto_message_id` ='".$auto_message_id."' ;";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		echo "2";
	}
} 
/* End of file democontroller.php */ 